<?php 
$this->headTitle('Thêm Tin', 'append');
?>
<div class="dashboard-menu dashboard-admin">
    <h1>Thêm Tin</h1>
</div>
<div class="row placeholders">
    <div class="col-md-12">
        <div class="widget-big">
            <div class="search-result text-left">
                <?php
                    if(isset($this->mess)){
                        if($this->flag == 'success')
                            echo '<div class="bg-success">',$this->mess,'</div>';
                        elseif($this->flag == 'alert')
                            echo '<div class="bg-warning">',$this->mess,'</div>';
                        else
                            echo '<div class="bg-danger">',$this->mess,'</div>';
                    }
                ?>                
                <form id="form" class="form-option" method="post" action="" enctype="multipart/form-data" name="form">
                    <dl class="zend_form">
                        <?php echo $this->form->id?>
                        <?php echo $this->form->userId?>
                        <?php echo $this->form->title?>
                        
                        
                        <?php echo $this->form->createDate?>
                        <?php echo $this->form->startDate?>
                        <?php echo $this->form->endDate?>                                                                        
                        
                        <?php echo $this->form->content?>
                        <?php echo $this->form->transaction?>                        
                        <?php echo $this->form->estate?>
                        <?php echo $this->form->area?>
                        <?php echo $this->form->price?>                        
                        <?php echo $this->form->unit?>                                                                                                                         
                        
                        <?php echo $this->form->total?>
                        
                        <dt id="otherInfo"><label></label></dt>
                        <dd id="otherInfo">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"><label>Thông tin khác</label></div>
                                    <div class="fieldset-content">
                                    <ul id="listother">
                                        <?php                                                                                                                             
                                            foreach ($this->other as $info){                                                
                                                if($info['groupInfo'] == 'otherInfo' && $info['elementType'] == 'text'){                                                    
                                        ?>                                                
                                                <li><label><?php echo $this->form->$info['id']->getLabel()?></label><?php echo $this->form->$info['id']?></li>
                                        <?php                                                
                                                }
                                            }
                                        ?>                                                                                                                          
                                        <li><label>Hướng nhà</label>
                                            <select name="1" style="width: 180px;">
                                            <?php foreach($this->car as $row){?>
                                            <option value="<?php echo $row?>"><?php echo $row;?></option>
                                            <?php }?>
                                            </select></li>
                                        <li><label>Hướng ban công</label>
                                            <select name="2" style="width: 180px;">
                                            <?php foreach($this->car as $row){?><option value="<?php echo $row?>"><?php echo $row;?></option><?php }?>
                                            </select></li>
                                    <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>                                                
                        
                        <dt id="otherUtility"><label></label></dt>
                        <dd id="otherUtility">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"><input type="checkbox" name="" id="check_all_resource_default"/><label>Tiện ích khác</label></div>
                                    <div class="fieldset-content">
                                    <ul>
                                    <?php                                     
                                        foreach ($this->other as $info){
                                            if($info['groupInfo'] == 'otherUtility'){
                                    ?>
                                            <li><?php echo $this->form->$info['id']?><label><?php echo $this->form->$info['id']->getLabel()?></label></li><!--<input type='checkbox' class='resourceIdAdmin' name='utility[]' value='{$info['id']}'/>-->
                                    <?php                                                
                                            }
                                        }
                                    ?>                                                                        
                                    <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>
                              
                              
                        <dt id="otherUtility"><label></label></dt>
                        <dd id="otherUtility">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"><label>Thông Tin Cơ Bản</label></div>
                                    <div class="fieldset-content">
                                    <ul>                                    
                                        <li><?php echo $this->form->city?></li>                                                                                                            
                                        <li><?php echo $this->form->district?></li>
                                        <li><?php echo $this->form->ward?></li>
                                        <li><?php echo $this->form->ward?></li>
                                        <li><?php echo $this->form->address?></li>
                                        <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>                                              
                        
                        <dt id="otherUtility"><label></label></dt>
                        <dd id="otherUtility">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"><label>Thông tin liên hệ</label></div>
                                    <div class="fieldset-content">
                                    <ul>                                    
                                        <li><?php echo $this->form->contact?></li>                                                                                                            
                                        <li><?php echo $this->form->mobile?></li>
                                        <li><?php echo $this->form->email?></li>
                                        <li><?php echo $this->form->phoneContact?></li>
                                        <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>
                                                    
                        <?php echo $this->form->status?>                                  
                        <?php echo $this->form->viewer?>
                                                
                        <dd id="submit-element">
                            <?php echo $this->form->submit?>
                            <?php echo $this->form->reset?>
                            <!--<input value="Thêm mới" class="button" type="submit" />
                            <input value="Reset" class="button" type="reset"/>-->
                        </dd>
                    </dl>
                </form>
            </div>
        </div>        
    </div>
</div></select><!--cho thue 0, bán 1 !-->
<script type="text/javascript">CKEDITOR.replace('content');</script>
<script type="text/javascript">
    $(document).ready(function(){                
        $('select#estate').append(new Option('--- Loại Bất Động Sản ---','-1'));
        $('select#district').append(new Option('--- Quận/Huyện ---','-1'));
        $('select#ward').append(new Option('--- Phường/Xã ---','-1'));
        $('select#project').append(new Option('--- Dự án ---','-1'));
        $('select#unit').append(new Option('--- Chọn đơn vụ ---','-1'));
    });
</script>
<script type="text/javascript">    
    $("#city").change(function(){
        $('select#ward option[value!="-1"]').remove();        
        $('#address').val( $(this).parent().parent().find('#city option:selected').text() );
        province=$(this).val();
        $.ajax({
            type: "POST",
            url: "/admin/location/getdistrict/province/"+province,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#district option[value!="-1"]').remove();
                for(var key in data){                        
                    $('select#district').append(new Option(data[key],key));
                }                                    
            },
        });
    }); 
    $("#district").change(function(){
        provinceName=$(this).parent().parent().find('#city option:selected').text();
        districtName=$(this).parent().parent().find('#district option:selected').text();
        $('#address').val(districtName+', '+provinceName);
        district=$(this).val();         
        $.ajax({
            type: "POST",
            url: "/admin/location/getward/district/"+district,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#ward option[value!="-1"]').remove();
                for(var key in data){   
                    $('select#ward').append(new Option(data[key],key));
                }                                    
            },
        });
        $.ajax({
            type: "POST",
            url: "/admin/project/getproject/district/"+district,                                            
            dataType: 'json',                
            success: function(data) {
                //alert(JSON.stringify(data));
                $('select#project option[value!="-1"]').remove();
                for(var key in data){   
                    $('select#project').append(new Option(data[key],key));
                }                                                   
            },
            error: function(){
                alert('failed');
            } 
        });
    });
    $("#ward").change(function(){
         //wardName=$("#ward :selected").text();
         //wardVal=$(this).val();
         //type=wardVal.split('_');
         //provinceName=$(this).parent().parent().find('#city option:selected').text();
         //districtName=$(this).parent().parent().find('#district option:selected').text();
         //$('#address').val(' '+type[1]+' '+wardName+', '+districtName+', '+provinceName);         
    });
    $("#project").change(function(){
        provinceName=$(this).parent().parent().find('#city option:selected').text();
        districtName=$(this).parent().parent().find('#district option:selected').text();
        projectName=$("#project :selected").text();                  
        $('#address').val('Dự án '+ projectName +', '+districtName+', '+provinceName);
        
    });
    $("#transaction").change(function(){     
        var tran=$(this).val();        
        $.ajax({
            type: "POST",
            url: "/admin/estate/getestate/transaction/"+tran,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#estate option[value!="-1"]').remove();
                for(var key in data){                     
                    $('select#estate').append(new Option(data[key],key));
                }                                    
            },            
        });
        $.ajax({
            type: "POST",
            url: "/admin/estate/getunit/transaction/"+tran,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#unit option[value!="-1"]').remove();
                for(var key in data){                       
                    $('select#unit').append(new Option(data[key],tran+key));
                }                                    
            },
        });         
    });
    $('#estate').change(function(){
        var estate=$(this).val();                        
        $.ajax({
            type: "POST",
            url: "/admin/estate/getutility/estate/"+estate,                                                  
            dataType: 'json',                           
            success: function(data) {
                //alert(JSON.stringify(data));                                                                            
                for(var key in data){
                    if(key=='otherInfoShow'){
                        for(var key in data['otherInfoShow']){
                             $('[name='+data['otherInfoShow'][key]+']').parent().attr('style','') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherInfoHide'){
                        for(var key in data['otherInfoHide']){
                             $('[name='+data['otherInfoHide'][key]+']').parent().attr('style','display: none;') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherUtilityShow'){
                        for(var key in data['otherUtilityShow']){
                             $('#'+data['otherUtilityShow'][key]).parent().attr('style','') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherUtilityHide'){
                        for(var key in data['otherUtilityHide']){
                             $('#'+data['otherUtilityHide'][key]).parent().attr('style','display: none;') ;                                                                 
                        }                                                        
                    }                                                                        
                }                
            }                          
        });                                             
    });        
                         
    $("#area").keyup(function(){
        area=$(this).val();
        price = $(this).parent().parent().find('#price').val();
        unit=$(this).parent().parent().find('#unit option:selected').val();
        after=$(this).parent().parent().find('#unit option:selected').text();
        if(area && price && unit){
            if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
            $(this).parent().parent().find('#total').val(price+' '+after);
            }
            else if(unit=='sale3'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
            }
            else if(unit=='sale4'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(result+' Triệu');
            }
            else if(unit='rent3'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
            }
            else{
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
            }
        }
    });
    $("#price").keyup(function(){
         price=$(this).val();
         area = $(this).parent().parent().find('#area').val();
         unit=$(this).parent().parent().find('#unit option:selected').val();
         after=$(this).parent().parent().find('#unit option:selected').text();
         if(area && price && unit){
             if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
                 $(this).parent().parent().find('#total').val(price+' '+after);
             }
             else if(unit=='sale3'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
             }
             else if(unit=='sale4'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(result+' Triệu');
             }
             else if(unit='rent3'){
                  result=parseInt(area)*parseInt(price);
                  $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
             }
             else{
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
             }
         }
    });
     $("#unit").change(function(){
        var unit=$(this).val();
        area = $(this).parent().parent().find('#area').val();
        price = $(this).parent().parent().find('#price').val();
        after=$(this).parent().parent().find('#unit option:selected').text();
        if(unit=='rent5'|| unit =='sale5'){
            $(this).parent().parent().find('#total').val('');
            document.getElementById("total").disabled = true;
        }else{
            document.getElementById("total").disabled = false;
        } 
        if(area && price && unit){
             if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
                 $(this).parent().parent().find('#total').val(price+' '+after);
             }
             else if(unit=='sale3'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
             }
             else if(unit=='sale4'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(result+' Triệu');
             }
             else if(unit='rent3'){
                  result=parseInt(area)*parseInt(price);
                  $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
             }
             else{
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
             }
         }
     });              
</script>